package com.xxg.fileshand.pop

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.animator.PopupAnimator
import com.lxj.xpopup.core.CenterPopupView
import com.xxg.fileshand.R
import com.xxg.fileshand.app.SetInfo
import com.xxg.fileshand.base.ext.clickWithTrigger
import com.xxg.fileshand.base.ext.dataConvert
import com.xxg.fileshand.base.ext.getApiService
import com.xxg.fileshand.base.launch
import com.xxg.fileshand.databinding.PopBorrowReturnBinding
import com.xxg.fileshand.greendao.GreenDaoManager
import com.xxg.fileshand.type.OperateType
import java.util.*

/**
 * Create by Sxl on 2023/8/10.
 */
internal class PopBorrowOrReturn  //注意：自定义弹窗本质是一个自定义View，但是只需重写一个参数的构造，其他的不要重写，所有的自定义弹窗都是这样。
    (context: Context, var type: Int, var onConfirmListener: OnConfirmListener) :
    CenterPopupView(context) {

    var mBinding: PopBorrowReturnBinding? = null

    // 返回自定义弹窗的布局
    override fun getImplLayoutId(): Int {
        return R.layout.pop_borrow_return
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    override fun onCreate() {
        super.onCreate()
        mBinding = PopBorrowReturnBinding.bind(popupImplView)
        mBinding?.tvTitle?.text =
            if (type == OperateType.OPERATE_TYPE_BORROW.type) "借阅信息" else "归还信息"
        mBinding?.etReason?.visibility = if (type == OperateType.OPERATE_TYPE_BORROW.type) {
            VISIBLE
        } else {
            GONE
        }
        mBinding?.tvCancel?.clickWithTrigger {
            (context as AppCompatActivity).finish()
        }
        mBinding?.tvConfirm?.clickWithTrigger {
            val readerId = mBinding!!.etReaderId.text.toString()
            val readerName = mBinding!!.etReaderName.text.toString()
            if (readerId.isNullOrEmpty() && readerName.isNullOrEmpty()) {
                ToastUtils.showShort("请输入人员Id或信息之一")
                return@clickWithTrigger
            }
            if (type == OperateType.OPERATE_TYPE_BORROW.type && mBinding!!.etReason.text.isNullOrEmpty()) {
                ToastUtils.showShort("请输入借阅原因")
                return@clickWithTrigger
            }
            if (SetInfo.isOffline()) {
                if (readerId.isNotEmpty()) {
                    GreenDaoManager.findBorrowDTO(readerId)
                } else {
                    GreenDaoManager.findBorrowDTO4Name(readerName)
                }?.let {
                    onConfirmListener?.onConfirm(it.cardId, it.cardName, mBinding!!.etReason.text.toString())
                    dismiss()
                } ?: ToastUtils.showShort("未查询到人员信息")
            } else {
                (context as AppCompatActivity).launch({
                    val readRp = getApiService().outboundValidReader(
                        if (readerId.isNotEmpty()) {
                            mapOf(
                                "reqId" to UUID.randomUUID().toString(),
                                "deviceId" to DeviceUtils.getUniqueDeviceId(),
                                Pair("borrowerId", readerId),
//                                "borrowerReason" to
                            )
                        } else {
                            mapOf(
                                "reqId" to UUID.randomUUID().toString(),
                                "deviceId" to DeviceUtils.getUniqueDeviceId(),
                                Pair("borrowerBy", readerName),
                            )
                        },
                    ).dataConvert()
                    if (readRp.result > 0) {
                        onConfirmListener?.onConfirm(
                            readRp.borrowerId, readRp.borrowerBy,
                            mBinding!!.etReason.text.toString()
                        )
                        dismiss()
                    } else {
                        ToastUtils.showShort(readRp.msg)
                    }
                })
            }
        }
    }

    // 设置最大宽度，看需要而定，
    override fun getMaxWidth(): Int {
        return super.getMaxWidth()
    }

    // 设置最大高度，看需要而定
    override fun getMaxHeight(): Int {
        return super.getMaxHeight()
    }

    // 设置自定义动画器，看需要而定
    override fun getPopupAnimator(): PopupAnimator {
        return super.getPopupAnimator()
    }

    override fun getPopupWidth(): Int {
        return 0
    }

    override fun getPopupHeight(): Int {
        return 0
    }

    interface OnConfirmListener {
        fun onConfirm(readId: String, readName:String, toString: String)
    }

}